<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Request;

class Question extends Model
{
		/* 添加问题API */
    public function add()
    {
    	// dd(Request::all());//打印Request看看
    	/* 检查用户是否登录 */
    	if (!user_ins()->is_logged_in())
    		return ['status' => 0, 'msg' => 'Login Require'];
    		/* 检查是否传递title参数,建议封装Request::get(),Request::All()方法到辅助函数,当前文件就不需要use Request */
    		if (!Request::get('title'))
	    		return ['status' => 0, 'msg' => 'Required title'];
	    	$this->title = Request::get('title');
	    	/* 如果存在desc就添加进来 */
	    	if (Request::get('desc'))
	    	$this->desc = Request::get('desc');

	    	/* 指定id为当前session中的user_id */
	    	$this->user_id = session('user_id');
	    	/* 插入数据库，成功则返回状态加用户id */
	    	return $this->save()?
	    	['status' => 1, 'id' => $this->id]:
	    	['status' => 0, 'msg'=> 'DB insert Failed'];
    }

    /* 更新问题API */
    public function change()
    {
    	if (user_ins()->is_logged_in() == false)
    		return ['status' => 0, 'msg' => 'Login Require'];

    	/* 参数必须带questions表中的id,指定更新的数据,这里将id保存到question变量中 */
    	if (!Request::get('id'))
    		return ['status' => 0, 'msg' => 'id is Require'];
 			
 			/* 获取指定id的Model,获取失败返回状态码 */
    	$question = $this->find(Request::get('id'));

    	/* 判断问题是否存在,不判断也行,在$question对比session['user_id']步骤也会出错 */
    	if (!$question)
				return ['status' => 0, 'msg' => 'Question Not Exists'];

    		/* 根据实际业务逻辑，判断是否只有发布者能做更新操作(还记得先过滤否定吗)
				 	 $questions表中的外键user_id == session中的user_id就可以执行更新操作*/
    	 
			if ($question->user_id != session('user_id'))
				return ['status' => 0, 'msg' => 'Persission Denied'];

			/* 重新设置记录值,如不设置title,据说只修改desc就会把title置空(我验证是不会的) */
			if (Request::get('title'))
				$question->title = $Request::get('title');

			if (Request::get('desc'))
				$question->desc = Request::get('desc');

			/* 保存新数据到数据库 */
			return $question->save()?
			['status' => 1]:
			['status' => 0, 'msg' => 'DB Update Failed'];
    }

    public function read()
    {
    	/* 请求参数是否有id,如果有,说明客户想查看某条信息,直接返回id所在行 */
    	if (Request::get('id'))
    		return ['status' => 1, 'data' => $this->find(Request::get('id'))];

    	/* 参数带不带id,默认每页显示$limit条 */
    	/* 参数带不带page,默认显示第一页 */
    	$limit = Request::get('limit')?:10;
    	$skip  = (Request::get('page')?Request::get('page')-1:0) * $limit;

    	/* 构建query并返回collection数据(无论数组还是对象，自动转换为collection格式的json数据) */
    	$res = $this
    	->orderBy('created_at')
    	->limit($limit)
    	->skip($skip)
    	->get(['id', 'title', 'desc', 'user_id', 'created_at'])
    	->keyBy('id');		
    	//get()用于转换结果集为Collection,默认显示全部字段,传递数组指定字段,类似数组,很重要的一个类
    	//->keyBy('id')可以指定记录的键,并把data从数组转换为对象供前端使用
    	return ['status' => 1, 'data' => $res];
    }

    public function remove()
    {
    	/* 检查用户是否登陆 */
    	if (!user_ins()->is_logged_in())
    		return ['status' => 0, 'msg' => 'Login Require'];

    	/* 检查是否有传参id */
    	if (!Request::get('id'))
    		return ['status' => 0, 'msg' => 'id is Required'];

    	/* 获取传参id所对应的Model,数据表中应该有该id */
    	$question = $this->find(Request::get('id'));
    	if (!$question)
    		return ['status' => 0, 'msg' => 'Question Not Exists'];

    	/* 检查当前用户是否为问题的所有者 */
    	if (session('user_id') != $question->user_id)
    		return ['status' => 0, 'msg' => 'Permission Denied'];

    	return $question->delete()?
    	['status' => 1]:
    	['status' => 0, 'msg' => 'DB Delete Failed'];

    	/* 业务逻辑：只有管理员或者所有者可以删除 */
    }
}